package com.whw.fy.service.impl;

import com.whw.fy.entity.Message;
import com.whw.fy.mapper.*;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import com.whw.fy.entity.Supervisor;
import com.whw.fy.service.SupervisorService;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

@Service
public class SupervisorServiceImpl implements SupervisorService {

    @Resource
    private SupervisorMapper supervisorMapper;
    @Resource
    private StudentMapper studentMapper;
    @Resource
    private StaffMapper staffMapper;
    @Resource
    private LessonMapper lessonMapper;
    @Resource
    private RoomMapper roomMapper;

    @Override
    public Message deleteByPrimaryKey(Integer supervisorId) {
        return supervisorMapper.deleteByPrimaryKey(supervisorId) > 0 ? Message.success(null) : Message.fail(null);
    }

    @Override
    public Message insert(Supervisor record) {
        return supervisorMapper.insert(record) > 0 ? Message.success(null) : Message.fail(null);
    }

    @Override
    public Message insertSelective(Supervisor record) {
        return supervisorMapper.insertSelective(record) > 0 ? Message.success(null) : Message.fail(null);
    }

    @Override
    public Message selectByPrimaryKey(Integer supervisorId) {
        return Message.success(null).add(supervisorMapper.selectByPrimaryKey(supervisorId));
    }

    @Override
    public Message updateByPrimaryKeySelective(Supervisor record) {
        return supervisorMapper.updateByPrimaryKeySelective(record) > 0 ? Message.success(null) : Message.fail(null);
    }

    @Override
    public Message updateByPrimaryKey(Supervisor record) {
        return supervisorMapper.updateByPrimaryKey(record) > 0 ? Message.success(null) : Message.fail(null);
    }

    @Override
    public Message selectAll() {
        List<Supervisor> supervisorList = supervisorMapper.selectAll();
        List<Map> mapList = new LinkedList<>();
        for(Supervisor supervisor : supervisorList) {
            HashMap map = new HashMap();
            map.put("supervisor",supervisor);
            if(supervisor.getSupervisorStudentId()!=null)
                map.put("supervisorStudentId",studentMapper.selectByPrimaryKey(supervisor.getSupervisorStudentId()).getStudentName());
            else map.put("supervisorStudentId",null);
            if(supervisor.getSupervisorTeacherId()!=null)
                map.put("supervisorTeacherId",staffMapper.selectByPrimaryKey(supervisor.getSupervisorTeacherId()).getStaffName());
            else map.put("supervisorTeacherId",null);
            if(supervisor.getSupervisorStaffId()!=null)
                map.put("supervisorStaffId",staffMapper.selectByPrimaryKey(supervisor.getSupervisorStaffId()).getStaffName());
            else map.put("supervisorStaffId",null);
            if(supervisor.getSupervisorLessonId()!=null)
                map.put("supervisorLessonId",lessonMapper.selectByPrimaryKey(supervisor.getSupervisorLessonId()).getLessonName());
            else map.put("supervisorLessonId",null);
            if(supervisor.getSupervisorRoomId()!=null)
                map.put("supervisorRoomId",roomMapper.selectByPrimaryKey(supervisor.getSupervisorRoomId()).getRoomLoc());
            else map.put("supervisorRoomId",null);

            mapList.add(map);
        }
        return Message.success(null).add(mapList);
    }

    @Override
    public int batchInsert(List<Supervisor> list) {
        return supervisorMapper.batchInsert(list);
    }
}

